package com.swcw.platform.constants;

import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.swcw.platform.utils.HttpTool;
import com.swcw.platform.utils.RedisAPI;
import com.swcw.platform.utils.StringUtils;

import net.sf.json.JSONObject;

/**
 * 拦截标识的接口
 * 
 * @see:
 * @Company:江苏鸿信系统集成有限公司微信开发组
 * @author 杨坚
 * @Time 2016年11月18日
 * @version 1.0v
 */
public class Interceptor implements HandlerInterceptor {


	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		boolean reply = false;
		// 登录密钥
		String authorizeKey = null;
		if (StringUtils.isNotEmpty(request.getParameter("params"))) {
			// 获取传值
			JSONObject requstJson = JSONObject.fromObject(URLDecoder.decode(request.getParameter("params"), "UTF-8"));
			// 是否包含
			if (requstJson.containsKey("authorizeKey")) {
				authorizeKey = requstJson.getString("authorizeKey");
			}
		}
		// 请求接口
		String reqLink = request.getRequestURI();
		// appkey是否为空
		if (StringUtils.isNotEmpty(authorizeKey)) {
			// 验证是否为菜单接口调用
			if (reqLink.contains("com/plaftorm/menu")) {
				// 参数
				JSONObject params = new JSONObject();
				params.put("reqIp", HttpTool.getIpAddr(request));
				params.put("loginName", authorizeKey);
				params.put("moduleName", reqLink);
				// 添加操作信息
			}
			// 验证鉴权是否超时
			reply = unifyEntranceToisAuthorize(request, authorizeKey);
			if (!reply) {
				// 鉴权失败 返回信息
				respondMSG(request, response);
			} else {
				return reply;
			}
		} else if (reqLink.contains("com/plaftorm/interface/unifyEntrance.do")) {// 获取鉴权可以免校验
			String[] methods = { "get", "qrcode" };
			for (String method : methods) {
				if (method.equals(request.getParameter("method"))) {
					return true;
				}
			}
		} else {
			// 鉴权失败 返回信息
			respondMSG(request, response);
		}
		return reply;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}

	/**
	 * 判断传送的鉴权是否超时及签名
	 * 
	 * @param authorizeKey
	 *            登录密钥
	 * @return
	 * @author 杨坚
	 * @Time 2016年11月18日
	 * @version 1.0v
	 */
	public boolean unifyEntranceToisAuthorize(HttpServletRequest request, String authorizeKey) {
		try {
			// 判断是否存在
			boolean exists = RedisAPI.existsStr(authorizeKey);
			if (exists) {
				return true;
			}
		} catch (Exception e) {
			System.out.println("=======判断传送的鉴权是否超时异常========");
		}
		return false;
	}

	/**
	 * 返回数据
	 * 
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author 杨坚
	 * @Time 2016年11月18日
	 * @version 1.0v
	 */
	public void respondMSG(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		PrintWriter out = response.getWriter();
		StringBuilder builder = new StringBuilder();
		builder.append("<script type=\"text/javascript\">");
		builder.append("alert('鉴权过期，请重新登录！');");
		builder.append("window.top.location.href='");
		builder.append(OwnConstant.WECHAT_PROJECT_LINK);
		builder.append("';");
		builder.append("</script>");
		out.print(builder.toString());
	}

}
